package com.jwthhealth_pub.old.subscribe;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.CardView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.LogUtil;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.api.model.CommonRespone;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.common.ImageLoader;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;
import com.jwthhealth_pub.old.subscribe.model.HomeMsgSubscribeInfoModel;
import com.jwthhealth_pub.old.subscribe.model.SubscribeList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.jwthhealth_pub.common.LogUtil.makeLogTag;

/**
 * 预约详情
 * Created by Administrator on 2017/7/18.
 */

public class SubscribeInfoActivity extends BaseActivity {

    private static final String TAG = makeLogTag(SubscribeInfoActivity.class);

    @BindView(R.id.title_layout)
    TitleLayout titleLayout;
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.cb_one)
    CardView cbOne;
    @BindView(R.id.shop_img)
    ImageView shopImg;
    @BindView(R.id.shop_name)
    TextView shopName;
    @BindView(R.id.shop_phone)
    TextView shopPhone;
    @BindView(R.id.shop_add)
    TextView shopAdd;
    @BindView(R.id.shop_project)
    TextView shopProject;
    @BindView(R.id.shop_date)
    TextView shopDate;
    @BindView(R.id.shop_time)
    TextView shopTime;
    @BindView(R.id.shop_identify_name)
    TextView shopIdentifyName;
    @BindView(R.id.shop_identify_phone)
    TextView shopIdentifyPhone;
    @BindView(R.id.shop_identify_id)
    TextView shopIdentifyId;
    @BindView(R.id.btn_resubscribe)
    Button btnResubscribe;
    @BindView(R.id.btn_subscribe_confirm)
    Button btnSubscribeConfirm;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.top_hint_layout)
    RelativeLayout topHintlayout;
    @BindView(R.id.subscribe_status)
    TextView subscribeStatus;

    private SubscribeList.DataBean mData;
    private Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subscribe_info);
        ButterKnife.bind(this);

        mIntent = getIntent();
        mData = mIntent.getParcelableExtra(Constant.DATA);
        boolean ishintshow = mIntent.getBooleanExtra("ishintshow", false);
        topHintlayout.setVisibility(ishintshow ? View.VISIBLE:View.GONE);
        subscribeStatus.setVisibility(!ishintshow ? View.VISIBLE:View.GONE);
//        btnSubscribeConfirm.setVisibility(mIntent.getBooleanExtra("cancancel", false)?ViewHolder.VISIBLE:ViewHolder.GONE);
        if (mData == null) {
            String id = mIntent.getStringExtra("index");
            if (id == null) return;
            LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
            if (userinfoModel == null || userinfoModel.getId() == null) return;
            Call<HomeMsgSubscribeInfoModel> _call = ApiHelper.getOneSubscribeInfo(id, userinfoModel.getId(), userinfoModel.getAndroidtoken());
            _call.enqueue(new Callback<HomeMsgSubscribeInfoModel>() {
                @Override
                public void onResponse(@NonNull Call<HomeMsgSubscribeInfoModel> call, @NonNull Response<HomeMsgSubscribeInfoModel> response) {
                    HomeMsgSubscribeInfoModel body = response.body();
                    if (body == null) return;
                    String code = body.getCode();
                    if (code.equals("0")) {
                        HomeMsgSubscribeInfoModel.DataBean data = body.getData();
                        initData(data);
                    } else {
                        String msg = body.getMsg();
                        if (msg != null)
                            LogUtil.e(body.getMsg(), TAG);
                    }
                }

                @Override
                public void onFailure(@NonNull Call<HomeMsgSubscribeInfoModel> call, @NonNull Throwable t) {
                    LogUtil.e(t.toString(), TAG);
                }
            });
        } else {
            initData();
        }




    }

    private void initData(HomeMsgSubscribeInfoModel.DataBean _data) {
        ImageLoader.picasso(_data.getPic(), shopImg);
        shopName.setText(_data.getName());
        shopProject.setText("预约项目:" + _data.getPname() + "");
        shopDate.setText("预约日期:" + _data.getAp_date() + "");
        shopTime.setText("预约时间:" + _data.getAtime() + "");
        shopIdentifyName.setText("预约时间:" + _data.getAname() + "");
        shopIdentifyPhone.setText("联系电话:" + _data.getAphone() + "");
        shopIdentifyId.setText("身份证号:" + _data.getNo_card() + "");
        shopPhone.setText("客服电话:" + _data.getKf_phone() + "");
        shopAdd.setText("地址:" + _data.getPos() + "");

        String astatus = _data.getAstatus();
        if (astatus.equals("4") || astatus.equals("3")) {
            btnResubscribe.setVisibility(View.GONE);
            btnSubscribeConfirm.setVisibility(View.GONE);
        }

        Resources resources = getResources();

        if (mIntent != null ) {
            String msgmsg = mIntent.getStringExtra("msgmsg");
            if (msgmsg != null) {
                subscribeStatus.setText(msgmsg);
                return;
            }
        }

        switch (astatus) {
            case "1":
                //正在审核
                tvStatus.setText(resources.getString(R.string.subscribe_info_status_one));
                subscribeStatus.setText(resources.getString(R.string.subscribe_info_status_one));
                break;
            case "2":
                //预约成功
//                tvStatus.setText(resources.getString(R.string.subscribe_info_status_two));
//                subscribeStatus.setText(resources.getString(R.string.subscribe_info_status_two));
                tvStatus.setText(_data.getAstatus_desc());
                subscribeStatus.setText(_data.getAstatus_desc());
                break;
            case "3":
                //已完成
//                tvStatus.setText(resources.getString(R.string.subscribe_info_status_thi));
//                subscribeStatus.setText(resources.getString(R.string.subscribe_info_status_thi));
                tvStatus.setText(_data.getAstatus_desc());
                subscribeStatus.setText(_data.getAstatus_desc());
                break;
            case "4":
//                tvStatus.setText(resources.getString(R.string.subscribe_info_status_for));
//                subscribeStatus.setText(resources.getString(R.string.subscribe_info_status_for));
                tvStatus.setText(_data.getAstatus_desc());
                subscribeStatus.setText(_data.getAstatus_desc());
                //已取消
                break;
            default:break;
        }
    }
    
    private void initData() {
        ImageLoader.picasso(mData.getPic(), shopImg);
        shopName.setText(mData.getName());
        shopProject.setText("预约项目:" + mData.getPname() + "");
        shopDate.setText("预约日期:" + mData.getAp_date() + "");
        shopTime.setText("预约时间:" + mData.getAtime() + "");
        shopIdentifyName.setText("预约时间:" + mData.getAname() + "");
        shopIdentifyPhone.setText("联系电话:" + mData.getAphone() + "");
        shopIdentifyId.setText("身份证号:" + mData.getNo_card() + "");
        shopPhone.setText("客服电话:" + mData.getKf_phone() + "");
        shopAdd.setText("地址:" + mData.getPos() + "");

        String astatus = mData.getAstatus();
        if (astatus.equals("4") || astatus.equals("3")) {
            btnResubscribe.setVisibility(View.GONE);
            btnSubscribeConfirm.setVisibility(View.GONE);
        }

        Resources resources = getResources();
        switch (astatus) {
            case "1":
                //正在审核
                tvStatus.setText(resources.getString(R.string.subscribe_info_status_one));
                break;
            case "2":
                //预约成功
                tvStatus.setText(resources.getString(R.string.subscribe_info_status_two));
                break;
            case "3":
                //已完成
                tvStatus.setText(resources.getString(R.string.subscribe_info_status_thi));
                break;
            case "4":
                tvStatus.setText(resources.getString(R.string.subscribe_info_status_for));
                //已取消
                break;
            default:break;
        }
    }

    @Override
    protected void title() {
        String title = mIntent.getStringExtra("msgtitle");
        if (title != null && title.length()>0) {
            titleLayout.setTitle(title);
        }

        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                Intent intent = new Intent(SubscribeInfoActivity.this, SubscribeListActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    @OnClick({R.id.btn_resubscribe, R.id.btn_subscribe_confirm})
    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.btn_resubscribe:
                getalertDialog(true);
                break;
            case R.id.btn_subscribe_confirm:
                getalertDialog(false);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            Intent intent = new Intent(SubscribeInfoActivity.this, SubscribeListActivity.class);
            startActivity(intent);
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    private void getalertDialog(final boolean isResubscribe) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View dialogView = View.inflate(this, R.layout.subscribe_item_dialog_cancel, null);
        final EditText ed = dialogView.findViewById(R.id.ed);
        builder.setView(dialogView);

        if (isResubscribe) {
            builder.setMessage(R.string.subscribe_alert_resubscribe);
        } else {
            builder.setMessage(R.string.subscribe_alert_cancel);
        }

        builder.setPositiveButton(R.string.subscribe_alert_resubscribe_pos, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
                if (userinfoModel != null) {
                    Call<CommonRespone> call;
                    if (mData.getAstatus().equals("2")) {
                        String desc = ed.getText().toString().trim();
                        call = ApiHelper.cancelAppoinment(mData.getAid(), desc, userinfoModel.getId(), userinfoModel.getAndroidtoken());
                        ed.setVisibility(View.VISIBLE);
                    } else {
                        call = ApiHelper.cancelAppoinment(mData.getAid(), userinfoModel.getId(), userinfoModel.getAndroidtoken());
                        ed.setVisibility(View.GONE);
                    }


                    call.enqueue(new Callback<CommonRespone>() {
                        @Override
                        public void onResponse(Call<CommonRespone> call, Response<CommonRespone> response) {
                            final CommonRespone body = response.body();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    LogUtil.toast(body.getMsg());
                                    if (isResubscribe) {
                                        startActivity(new Intent(SubscribeInfoActivity.this, SubscribeActivity.class));
                                        SubscribeListActivity subscribeListActivity = new SubscribeListActivity();
                                        subscribeListActivity.finish();
                                        finish();
                                    } else {
                                        if (body.getCode().equals("0")) {
                                            tvStatus.setText(getResources().getString(R.string.subscribe_state_for));
                                        } else {
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    LogUtil.toast(body.getMsg());
                                                }
                                            });
                                        }

                                        finish();
                                    }
                                }
                            });
                        }

                        @Override
                        public void onFailure(Call<CommonRespone> call, Throwable t) {
                            LogUtil.e(t.toString(), TAG);
                        }
                    });
                }
            }
        });
        builder.setNegativeButton(R.string.subscribe_alert_resubscribe_neg, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
}
